<?php

namespace app\common\model\Order;

use app\common\model\Business\Business;
use app\common\model\Product\Product as ProductProduct;
use app\common\model\Product\Relation;
use think\Model;
use traits\model\SoftDelete;


class Product extends Model
{
    use SoftDelete;
    // 表名
    protected $name = 'order_product';
    // 忽略数据表不存在的字段
    protected $field = true;
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    //禁止写入时间字段
    protected $updateTime = false;
    // 软删除的字段
    protected $deleteTime = 'deletetime';
    // 设置返回的结果类型
    protected $resultSetType = 'collection';
    // 追加属性
    protected $append = [
        'comdate',
        'prop_list',
        'status_text',
        'comtime_text',
        'query_time_text',
        'query_num_text',
    ];
    public function business()
    {
        return $this->belongsTo(Business::class,'busid','id',[],'left')->setEagerlyType(0);
    }
    public function order()
    {
        return $this->belongsTo(Order::class,'orderid','id',[],'left')->setEagerlyType(0);
    }
    public function productProduct()
    {
        return $this->belongsTo(ProductProduct::class,'proid','id',[],'left')->setEagerlyType(0);
    }
    public function getComdateAttr($value,$data)
    {
        if(!isset($data['comtime']))return '';
        return date('Y-m-d',$data['comtime']);
    }
    public function getPropListAttr($value,$data)
    {
        if(!isset($data['attrs']))return null;
        return Relation::where('id','in',json_decode($data['attrs']))->column('value');
    }
    // public function getStatusTextAttr($value,$data)
    // {
    //     switch($data['status'])
    //     {
    //         case '0':
    //             return '未发货';
    //         case '1':
    //             return '已发货';
    //         case '2':
    //             return '已收货';
    //         case '3':
    //             return '已完成';
    //         case '-1':
    //             return '仅退款';
    //         case '-2':
    //             return '退款退货';
    //         case '-3':
    //             return '售后中(待退货)';
    //         case '-4':
    //             return '退货成功';
    //         case '-5':
    //             return '退货失败';
    //     }
    //     return '';
    // }
    public function getStatusList()
    {
        return ['0' => __('Unfilled'), '1' => __('Delivered'), '2' => __('Received'), '3' => __('Completed'), '-1' => __('Refund only'), '-2' => __('Refund and return'), '-3' => __('After sale'), '-4' => __('Success'), '-5' => __('Failure')];
    }


    public function getComtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['comtime']) ? $data['comtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getQueryTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['query_time']) ? $data['query_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setComtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setQueryTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }
    protected function getQueryNumTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['query_num']) ? $data['query_num'] : 0);
        return $value;
    }
}
